﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ScooterLand
{
    public partial class ProductForm : Form
    {
        Backend.Program controller = new Backend.Program();
        private int groupID;
        private int productID;

        public ProductForm()
        {
            InitializeComponent();
        }

        private void orderItem_Click(object sender, EventArgs e)
        {
            OrderForm oForm = new OrderForm();
            oForm.Show();
            this.Close();
        }

        private void customersItem_Click(object sender, EventArgs e)
        {
            CustomerForm cForm = new CustomerForm();
            cForm.Show();
            this.Close();
        }

        private void productsItem_Click(object sender, EventArgs e)
        {
            ProductForm pForm = new ProductForm();
            pForm.Show();
            this.Close();
        }

        private void employeesItem_Click(object sender, EventArgs e)
        {
            EmployeeForm eForm = new EmployeeForm();
            eForm.Show();
            this.Close();
        }

        private void invoicesItem_Click(object sender, EventArgs e)
        {
            InvoiceForm iForm = new InvoiceForm();
            iForm.Show();
            this.Close();
        }

        private void exitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ProductForm_Load(object sender, EventArgs e)
        {
            DataTable groupTable = controller.ListGroups();
            DataRow[] groupRows = groupTable.Select();

            foreach (DataRow row in groupRows)
            {
                string[] strGroups = { row[0].ToString(), row[1].ToString() };
                ListViewItem lvItem = new ListViewItem(strGroups);
                lvGroups.Items.Add(lvItem);
            }
        }

        private void btnGroupUpdate_Click(object sender, EventArgs e)
        {
            lvProducts.Items.Clear();
            groupID = Convert.ToInt32(lvGroups.SelectedItems[0].Text);
            txtGroupName.Text = lvGroups.SelectedItems[0].SubItems[1].Text;

            DataTable productTable = controller.ListProducts(groupID);
            DataRow[] productRows = productTable.Select();

            foreach (DataRow row in productRows)
            {
                string[] strProducts = { row[0].ToString(), row[1].ToString(), row[2].ToString(), row[3].ToString(), row[4].ToString() };
                ListViewItem lvItem = new ListViewItem(strProducts);
                lvProducts.Items.Add(lvItem);
            }
        }

        private void btnGroupRemove_Click(object sender, EventArgs e)
        {
            groupID = Convert.ToInt32(lvGroups.SelectedItems[0].Text);
            bool result = controller.RemoveGroup(groupID);

            if (result != true)
                MessageBox.Show("Det lykkedes ikke at fjerne den valgte varegruppe.");
            else
            {
                lvGroups.Items.Remove(lvGroups.SelectedItems[0]);
                bool pResult = controller.RemoveAllProducts(groupID);

                if (pResult != true)
                    MessageBox.Show("Det lykkedes ikke at fjerne alle produkter tilhørende varegruppen.");
                else
                    groupID = 0;
            }
        }

        private void btnGroupSave_Click(object sender, EventArgs e)
        {
            bool result = false;

            if (groupID != 0)
            {
                string groupName = txtGroupName.Text;

                result = controller.UpdateGroup(groupID, groupName);

                if (result != true)
                    MessageBox.Show("Der skete en fejl under opdateringen af varegruppen.");
                else
                {
                    lvGroups.Items.Remove(lvGroups.SelectedItems[0]);

                    string[] strGroup = { groupID.ToString(), groupName };
                    ListViewItem lvItem = new ListViewItem(strGroup);
                    lvGroups.Items.Add(lvItem);

                    txtGroupName.Text = "";
                    groupID = 0;
                }
            }
            else
            {
                string groupName = txtGroupName.Text;

                controller.CreateGroup(groupName, out result, out groupID);

                if (result != true)
                    MessageBox.Show("Handlingen kunne ikke udføres.");
                else
                {
                    string[] strGroup = { groupID.ToString(), groupName };
                    ListViewItem lvItem = new ListViewItem(strGroup);
                    lvGroups.Items.Add(lvItem);

                    txtGroupName.Text = "";

                    groupID = 0;
                }
            }
        }

        private void btnProductSave_Click(object sender, EventArgs e)
        {
            bool result = false;

            if (productID != 0)
            {
                string productName = txtProductName.Text;
                string productPrice = txtProductPrice.Text;
                string productOffer = txtProductOffer.Text;
                int productStock = Convert.ToInt32(txtProductStock.Text);
                string productWord = txtProductWord.Text;

                result = controller.UpdateProduct(productID, productName, productPrice, productOffer, productStock, productWord);

                if (result != true)
                    MessageBox.Show("Handlingen kunne ikke udføres.");
                else
                {
                    lvProducts.Items.Remove(lvProducts.SelectedItems[0]);

                    string[] strProducts = { productID.ToString(), productName, productPrice.Replace('.', ','), productOffer.Replace('.', ','), productStock.ToString(),  };
                    ListViewItem lvItem = new ListViewItem(strProducts);
                    lvProducts.Items.Add(lvItem);

                    txtProductName.Text = "";
                    txtProductPrice.Text = "0.00";
                    txtProductOffer.Text = "0.00";
                    txtProductStock.Text = "0";
                    txtProductWord.Text = "";

                    productID = 0;
                }
            }
            else
            {
                string productName = txtProductName.Text;
                string productPrice = txtProductPrice.Text;
                string productOffer = txtProductOffer.Text;
                int productStock = Convert.ToInt32(txtProductStock.Text);
                string productWord = txtProductWord.Text;
                
                controller.CreateProduct(groupID, productName, productPrice, productOffer, productStock, productWord, out result, out productID);
                
                if (result != true)
                    MessageBox.Show("Handlingen kunne ikke udføres.");
                else
                {
                    string[] strProduct = { productID.ToString(), productName, productPrice.ToString(), productOffer.ToString(), productStock.ToString() };
                    ListViewItem lvItem = new ListViewItem(strProduct);
                    lvProducts.Items.Add(lvItem);

                    txtProductName.Text = "";
                    txtProductPrice.Text = "0.00";
                    txtProductOffer.Text = "0.00";
                    txtProductStock.Text = "0";
                    txtProductWord.Text = "";

                    productID = 0;
                }

            }
        }

        private void btnProductUpdate_Click(object sender, EventArgs e)
        {
            productID = Convert.ToInt32(lvProducts.SelectedItems[0].Text);

            // Get Product Info
            string[] productArray = controller.GetProductInfo(productID);

            txtProductName.Text = productArray[0];
            txtProductPrice.Text = productArray[1].Replace(',', '.');
            txtProductOffer.Text = productArray[2].Replace(',', '.');
            txtProductStock.Text = productArray[3];
            txtProductWord.Text = productArray[4];
        }

        private void btnProductRemove_Click(object sender, EventArgs e)
        {
            productID = Convert.ToInt32(lvProducts.SelectedItems[0].Text);
            bool result = controller.RemoveProduct(productID);

            if (result != true)
                MessageBox.Show("Handlingen kunne ikke fuldføres.");
            else
            {
                productID = 0;
                lvProducts.Items.Remove(lvProducts.SelectedItems[0]);
            }
        }
    }
}
